package com.example.planetexploder.module.menu;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.example.planetexploder.adapter.MyAdapter_MenuEdit;
import com.example.planetexploder.R;
import com.example.planetexploder.bean.otherdata.MenuItem;

import java.util.ArrayList;

public class View_MneuEdit extends AppCompatActivity implements AdapterView.OnItemLongClickListener {

    private Presenter_Menu presenter_menu;
    private ListView mListView;//ListView控件
    private MyAdapter_MenuEdit mAdapter;
    private ArrayList<MenuItem> mListMenu;
    private boolean isShowDelete = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menuedit);
        presenter_menu = new Presenter_Menu(getApplicationContext());
        mListMenu = presenter_menu.create_Menu();


        mListView = findViewById(R.id.MenuEditListView);
        mListView.setOnItemLongClickListener(View_MneuEdit.this);//长按事件监听
        mAdapter = new MyAdapter_MenuEdit(View_MneuEdit.this, mListMenu);
        mListView.setAdapter(mAdapter);

        ListView listView = findViewById(R.id.MenuEditListView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                editNewListViewItem(View_MneuEdit.this,position);
                }
        });

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
        if (isShowDelete) {
            isShowDelete = false;
        } else {
            isShowDelete = true;
            mAdapter.setIsShowDelete(isShowDelete);
            mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                    presenter_menu.delete_Tag(mListMenu.get(position).getId());
                    deleteListViewItems(position);//删除选中项
                    refreshListView();//刷新ArrayList数组并重新绑定容器
                }

            });
        }
        mAdapter.setIsShowDelete(isShowDelete);//setIsShowDelete()方法用于传递isShowDelete值
        return true;
    }

    private void deleteListViewItems(int position) {
        ArrayList<MenuItem> newList = new ArrayList<MenuItem>();
        if (isShowDelete) {
            mListMenu.remove(position);
            isShowDelete = false;
        }
        newList.addAll(mListMenu);
        mListMenu.clear();
        mListMenu.addAll(newList);

        //重新将items设置为单击事件，实现绑定
        ListView listView = findViewById(R.id.MenuEditListView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                editNewListViewItem(View_MneuEdit.this,position);
            }
        });
    }

    /**
     * 功能：刷新ArrayList数组并重新绑定容器
     * 修改日期：2019.10.21
     */
    public void refreshListView() {

        mListMenu = presenter_menu.create_Menu();
        mAdapter = new MyAdapter_MenuEdit(View_MneuEdit.this, mListMenu);//重新绑定一次adapter
        mListView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();//刷新GridView
    }

    /**
     * 功能：实现编辑ListView中的item；
     * 修改日期：2019.10.21
     */
    public void editNewListViewItem(final Context context, int position){
        final EditText newText = new EditText(context);
        newText.setText(mListMenu.get(position).getTitle());
        new AlertDialog.Builder(context).setTitle("修改自定义标签")
                //监听保存键的点击行为
                .setView(newText).setNegativeButton("取消", null)
                .setPositiveButton("保存", new DialogInterface.OnClickListener() {
                    //保存摘抄集名字
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if(newText.getText().toString().length()> 0)
                        {
                            if(newText.getText().toString().length() >30){
                            Toast.makeText(View_MneuEdit.this,"您输入的文字字数已到上限", Toast.LENGTH_SHORT).show();}
                            else{
                                Toast.makeText(View_MneuEdit.this,newText.getText().toString(), Toast.LENGTH_SHORT).show();
                            }
                        }
                        else {
                            Toast.makeText(View_MneuEdit.this, "您输入文字字数不能为零", Toast.LENGTH_SHORT).show(); }
                    }
                }).show();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull android.view.MenuItem item) {
        if(item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
